package com.imooc.course.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.course.server.dao.MemberDao;
import com.imooc.course.server.dto.*;
import com.imooc.course.server.entity.Member;
import com.imooc.course.server.entity.MemberExample;
import com.imooc.course.server.enums.BusinessExceptionEnum;
import com.imooc.course.server.exception.BusinessException;
import com.imooc.course.server.service.MemberService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service("memberService")
public class MemberServiceImpl implements MemberService {

    private static final Logger log = LoggerFactory.getLogger(MemberServiceImpl.class);

    @Resource(name = "memberDao")
    private MemberDao memberDao;

    /**
     * 列表查询，供admin控制台使用
     * @param pageDto
     */
    @Override
    public void getList(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());

        MemberExample memberExample = new MemberExample();
        List<Member> memberList = memberDao.selectByExample(memberExample);

        PageInfo<Member> memberPageInfo = new PageInfo<>(memberList);
        pageDto.setTotal(memberPageInfo.getTotal());

        List<MemberDto> memberDtoList = CopyUtil.copyList(memberList, MemberDto.class);

        pageDto.setList(memberDtoList);
    }

    /**
     * 保存，id有值为更新，id无值为新增
     * @param memberDto
     */
    @Override
    public void save(MemberDto memberDto) {
        Member copy = CopyUtil.copy(memberDto, Member.class);
        String id = memberDto.getId() != null ? memberDto.getId().trim() : memberDto.getId();
        if (StringUtils.isEmpty(id)) {
            this.addMember(copy);
        }else{
            this.updateMember(copy);
        }
    }

    private void updateMember(Member copy) {
        memberDao.updateByPrimaryKeySelective(copy);
    }

    private void addMember(Member copy) {
        copy.setId(UuidUtil.getShortUuid());
        memberDao.insertSelective(copy);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id) {
        memberDao.deleteByPrimaryKey(id);
    }

    /**
     * 通过登录名查找用户
     * @param memberDto
     * @return
     */
    @Override
    public Member findByLoginName(MemberDto memberDto) {
        MemberDto dto = this.matchingMobile(memberDto.getMobile());
        if (dto == null) {
            return null;
        } else {
            return CopyUtil.copy(dto, Member.class);
        }
    }

    /**
     * 会员登录
     * @param memberDto
     * @return
     */
    @Override
    public LoginMemberDto login(MemberDto memberDto) {
        //验证登录信息是否和数据信息一致
        Member member = this.findByLoginName(memberDto);
        if (member == null) {
            log.info("不存在的登录名 = {}", memberDto.getMobile());
            throw new BusinessException(BusinessExceptionEnum.LOGIN_ERROR);
        } else {
            if (memberDto.getPassword().equals(member.getPassword())) {
                //登录成功的操作
                LoginMemberDto loginMemberDto = CopyUtil.copy(member, LoginMemberDto.class);
                return loginMemberDto;
            } else {
                //登录失败
                log.info("登录密码 = {}, 数据库密码 = {}",memberDto.getPassword(),member.getPassword());
                throw new BusinessException(BusinessExceptionEnum.LOGIN_ERROR);
            }
        }
    }

    /**
     * 匹配手机号
     * @param mobile
     * @return
     */
    @Override
    public MemberDto matchingMobile(String mobile) {
        MemberExample example = new MemberExample();
        example.createCriteria().andMobileEqualTo(mobile);
        List<Member> memberList = memberDao.selectByExample(example);
        if (CollectionUtils.isEmpty(memberList)) {
            return null;
        } else {
            return  CopyUtil.copy(memberList.get(0), MemberDto.class);
        }
    }

    /**
     * 重置密码
     * @param memberDto
     */
    @Override
    public void restPassword(MemberDto memberDto) {
        Member memberDb = findByLoginName(memberDto);
        if (memberDb == null) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_NOT_EXIST);
        } else {
            Member member = new Member();
            member.setPassword(memberDto.getPassword());
            MemberExample example = new MemberExample();
            example.createCriteria().andIdEqualTo(memberDb.getId())
                    .andMobileEqualTo(memberDb.getMobile());
            memberDao.updateByExampleSelective(member, example);
        }
    }


}
